/**
 * Copyright � 2012 GestureSoft. ALL RIGHTS RESERVED.
 * This file contains proprietary and GestureSoft CONFIDENTIAL Information.
 * Use, disclosure or reproduction is prohibited.
 * 
 * @fileName LocalDataBaseServices.as
 * @creationDate Aug 8, 2012
 * @author 
 * @version 1.0
 */
package com.gesturesoft.imultimax.model.proxy 
{
	import com.gesturesoft.imultimax.model.DataBaseManager;
	import com.gesturesoft.imultimax.model.dbmanager.DBEncryptor;
	import com.gesturesoft.imultimax.model.error.DataBaseError;
	import com.gesturesoft.imultimax.model.interfaces.IDatabaseManager;
	import com.gesturesoft.imultimax.model.proxy.interfaces.IDataBaseServices;
	import com.gesturesoft.imultimax.model.vo.TransactionVO;
	
	import mx.collections.ArrayCollection;

	public class LocalDataBaseServices implements IDataBaseServices
	{
		
		public function insertTransaction(transactionVO:TransactionVO):void
		{
			
			var iDataBaseManager:IDatabaseManager = new DataBaseManager();
			
			try
			{
				
				iDataBaseManager.insertValues(
					"INSERT INTO IM_BEFORE_LIST (bl_name, bl_contact, bl_amount, bl_type) " + 
					"VALUES('"+ 
					transactionVO.name +"','" + 
					transactionVO.contact +"','" + 
					transactionVO.amount +"','"+transactionVO.type+"')");
				
			} 
			catch(error:DataBaseError) 
			{
				
				throw error;
				
			}
			
		}
		
		public function getTransactions():ArrayCollection
		{
			
			var resultArrayCollection:ArrayCollection = null;
			var idataBaseManager:IDatabaseManager = new DataBaseManager();
			
			try
			{
				
				resultArrayCollection = idataBaseManager.getData("SELECT * FROM IM_BEFORE_LIST ORDER BY bl_date DESC");
				
			} 
			catch(error:DataBaseError) 
			{
				
				trace(error.message);
				throw error;
				
			}
			
			return resultArrayCollection;
			
		}
		
		public function saveForLater(transactionVO:TransactionVO):void
		{
			
			var iDataBaseManager:IDatabaseManager = new DataBaseManager();
			
			try
			{
				
				iDataBaseManager.insertValues(
					"INSERT INTO IM_SAVED_FOR_LATER (sfl_name, sfl_contact, sfl_amount, sfl_status, sfl_type) " + 
					"VALUES('"+ 
					transactionVO.name +"','" + 
					transactionVO.contact +"','" + 
					transactionVO.amount +"','"+transactionVO.status+"','"+transactionVO.type+"')");
				
			} 
			catch(error:DataBaseError) 
			{
				
				throw error;
				
			}
			
		}
		
		public function getSavedForLater():ArrayCollection
		{
			
			var resultArrayCollection:ArrayCollection = null;
			var iDataBaseManager:IDatabaseManager = new DataBaseManager();
			
			try
			{
				
				resultArrayCollection = iDataBaseManager.getData("SELECT * FROM IM_SAVED_FOR_LATER ORDER BY sfl_date DESC");
				
			} 
			catch(error:DataBaseError) 
			{
				
				throw error;
				
			}
			
			return resultArrayCollection;
			
		}
		
		public function getCompletedPayments():ArrayCollection
		{
			
			var resultArrayCollection:ArrayCollection = null;
			var iDataBaseManager:IDatabaseManager = new DataBaseManager();
			
			try
			{
				
				resultArrayCollection = iDataBaseManager.getData("SELECT * FROM IM_BEFORE_LIST ORDER BY bl_date DESC");
				
			} 
			catch(error:DataBaseError) 
			{
				
				throw error;
				
			}
			
			return resultArrayCollection;
			
		}
		
		public function getDistinctTransactions(value:String):ArrayCollection
		{
			
			var resultArrayCollection:ArrayCollection = null;
			var idataBaseManager:IDatabaseManager = new DataBaseManager();
			
			try
			{
				
				resultArrayCollection = idataBaseManager.getData("SELECT DISTINCT bl_contact,bl_name FROM IM_BEFORE_LIST WHERE bl_type='"+ DBEncryptor.encrypt(value) + "'");
				
			} 
			catch(error:DataBaseError) 
			{
				
				trace(error.message);
				throw error;
				
			}
			
			return resultArrayCollection;
			
		}
		
	}
	
}